import React,{
    PureComponent
} from 'react';

import {
    BackHandler
} from 'react-native';

import {
    Actions
} from 'react-native-router-flux';

const eventName = `hardwareBackPress`;

export default (Comp: Function, Props: any) => (
    class Layer extends PureComponent {
        get Props() {
            return {
                ...Props,
                ...this.props
            }
        }

        render() {
            return (
                <Comp {...this.Props}/>
            )
        }

        componentDidMount() {
            BackHandler.addEventListener(
                eventName, this.hardwareBackPress
            )
        }

        componentWillUnmount() {
            BackHandler.removeEventListener(
                eventName,this.hardwareBackPress
            )
        }

        private hardwareBackPress = () => {
            const {
                lockPage
            } = Props;

            if(!lockPage) {
                Actions.pop();
            }
            return true;
        }
    }
)